package com.umeng.analytics.pro;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class a {
    public a() {

    }

    private static ContentValues a(i arg4) {
        ContentValues v0 = new ContentValues();
        v0.put("key", arg4.a());
        v0.put("label", arg4.c());
        v0.put("count", Long.valueOf(arg4.g()));
        v0.put("value", Long.valueOf(arg4.f()));
        v0.put("totalTimestamp", Long.valueOf(arg4.e()));
        v0.put("timeWindowNum", arg4.h());
        return v0;
    }

    public static String a(SQLiteDatabase arg6) {
        SQLException v1_1;
        SQLException v5;
        Cursor v2 = null;
        String v0_2 = null;
        String v1 = null;
        try {
            arg6.beginTransaction();
            if (a.c(arg6, "aggregated_cache") > 0) {
                v2 = arg6.rawQuery("select * from aggregated_cache", null);
                if (v2.moveToLast()) {
                    v0_2 = v2.getString(v2.getColumnIndex("timeWindowNum"));
                } else {
                    v0_2 = v1;
                }
                arg6.setTransactionSuccessful();
                return v0_2;
            }
            v0_2 = "0";
        } catch (SQLException v0) {
            by.e("queryLastTimeWindowNumFromCache error " + v0.toString());
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
            arg6.endTransaction();
        }
        return "0";
    }

    public static JSONObject a(f arg11, SQLiteDatabase arg12) {
        JSONArray v3_1;
        Cursor v2_1 = null;
        Cursor v0_2;
        JSONObject v2;
        JSONObject v1 = null;
        try {
            v2 = new JSONObject();
            if (a.c(arg12, "system") <= 0) {
                return v2;
            }

            v0_2 = arg12.rawQuery("select * from system", null);
            while (true) {
                if (!v0_2.moveToNext()) {
                    return v2;
                }
                try {
                    String v4 = v0_2.getString(v0_2.getColumnIndex("key"));
                    if (v2.has(v4)) {
                        v3_1 = v2.getJSONArray(v4);
                        v2.remove(v4);
                    } else {
                        v3_1 = new JSONArray();
                    }

                    JSONObject v5 = new JSONObject();
                    v5.put("value", v0_2.getInt(v0_2.getColumnIndex("count")));
                    v5.put("ts", v0_2.getLong(v0_2.getColumnIndex("timeStamp")));
                    v3_1.put(v5);
                    v2.put(v4, v3_1);
                    continue;
                } catch (Exception v3) {
                    continue;
                }
            }
        } catch (SQLException v2_2) {
            arg11.a("faild", false);
            by.e("readAllSystemDataForUpload error " + v2_2.toString());
            return v1;
        } finally {
            if (v2_1 != null) {
                try {
                    v2_1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static void a(SQLiteDatabase arg4, String arg5, long arg6, long arg8) {
        ContentValues v0_2;
        try {
            int v0_1 = a.c(arg4, "system");
            int v1 = n.a().c();
            if (v0_1 < v1) {
                v0_2 = new ContentValues();
                v0_2.put("key", arg5);
                v0_2.put("timeStamp", Long.valueOf(arg8));
                v0_2.put("count", Long.valueOf(arg6));
                arg4.insert("system", null, v0_2);
                return;
            }

            if (v0_1 == v1) {
                v0_2 = new ContentValues();
                v0_2.put("key", "__meta_ve_of");
                v0_2.put("timeStamp", Long.valueOf(System.currentTimeMillis()));
                v0_2.put("count", Integer.valueOf(1));
                arg4.insert("system", null, v0_2);
            } else {
                a.d(arg4, "__meta_ve_of");
            }

            return;
        } catch (SQLException v0) {
            return;
        }
    }

    public static void a(SQLiteDatabase arg12, Map arg13, f arg14) {
        long v4_1;
        String v9;
        ContentValues v8;
        long v2;
        Object v0_2;
        long v6 = 0;
        int v4 = 0;
        Cursor v1 = null;
        try {
            v0_2 = arg13.get("__ag_of");
            if (v0_2 == null) {
                return;
            }

            String v5 = "system where key=\"__ag_of\"";
            v1 = arg12.rawQuery("select * from " + v5, null);
            v1.moveToFirst();
            v2 = v6;
            while (!v1.isAfterLast()) {
                if (v1.getCount() > 0) {
                    v4 = v1.getInt(v1.getColumnIndex("count"));
                    v2 = v1.getLong(v1.getColumnIndex("timeStamp"));
                    arg12.execSQL("delete from " + v5);
                }

                v1.moveToNext();
            }

            v8 = new ContentValues();
            v8.put("key", ((k) v0_2).c());
            v9 = "count";
            if (v4 == 0) {
                v4_1 = ((k) v0_2).e();
            } else {
                v4_1 = ((long) v4);
                v4_1 += ((k) v0_2).e();
            }
            v8.put(v9, Long.valueOf(v4_1));
            String v4_2 = "timeStamp";
            if (v2 == v6) {
                v2 = ((k) v0_2).d();
            }

            v8.put(v4_2, Long.valueOf(v2));
            arg12.insert("system", null, v8);
            arg14.a("success", false);
            if (v1 == null) {
                return;
            }
        } catch (SQLException v0) {
            by.e("save to system table error " + v0.toString());
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static void a(SQLiteDatabase arg2, boolean arg3, f arg4) {
        a.b(arg2, "system");
        a.b(arg2, "aggregated");
        if (!arg3) {
            a.b(arg2, "limitedck");
            arg4.a("success", false);
        }
    }

    public static void a(f arg4, SQLiteDatabase arg5, List arg6) {
        try {
            arg5.beginTransaction();
            if (a.c(arg5, "limitedck") > 0) {
                a.b(arg5, "limitedck");
            }

            Iterator v1 = arg6.iterator();
            while (v1.hasNext()) {
                Object v0_2 = v1.next();
                ContentValues v2 = new ContentValues();
                v2.put("ck", ((String) v0_2));
                arg5.insert("limitedck", null, v2);
            }

            arg5.setTransactionSuccessful();
            arg4.a("success", false);
        } catch (SQLException v0) {
            by.e("insertToLimitCKTable error " + v0.toString());
        } finally {
            arg5.endTransaction();
        }
    }

    public static boolean a(SQLiteDatabase arg4, f arg5) {
        boolean v0 = false;
        try {
            arg4.beginTransaction();
            if (a.c(arg4, "aggregated_cache") > 0) {
                arg4.execSQL("insert into aggregated(key, count, value, totalTimestamp, timeWindowNum, label) select key, count, value, totalTimestamp, timeWindowNum, label from aggregated_cache");
                arg4.setTransactionSuccessful();
                a.b(arg4, "aggregated_cache");
                arg5.a("success", false);
                return true;
            }
            arg5.a("faild", false);
        } catch (SQLException v1) {
            arg5.a(Boolean.valueOf(false), false);
            by.e("cacheToAggregatedTable happen " + v1.toString());
            return false;
        } finally {
            arg4.endTransaction();
            return v0;
        }
    }

    public static boolean a(SQLiteDatabase arg2, String arg3) {
        boolean v0_1;
        try {
            arg2.execSQL("drop table if exists " + arg3);
            v0_1 = true;
        } catch (SQLException v0) {
            by.e("delete table faild!");
            v0.printStackTrace();
            v0_1 = false;
        }

        return v0_1;
    }

    public static boolean a(SQLiteDatabase arg4, Collection<i> arg5) {
        try {
            arg4.beginTransaction();
            if (a.c(arg4, "aggregated_cache") > 0) {
                a.b(arg4, "aggregated_cache");
            }

            Iterator<i> v1 = arg5.iterator();
            while (v1.hasNext()) {
                arg4.insert("aggregated_cache", null, a(v1.next()));
            }
            arg4.setTransactionSuccessful();
            return true;
        } catch (SQLException v0_1) {
            by.e("insert to Aggregated cache table faild!");
            return false;
        } finally {
            arg4.endTransaction();
        }
    }

    public static boolean a(f arg5, SQLiteDatabase arg6, Collection<i> arg7) {
        try {
            arg6.beginTransaction();
            Iterator<i> v2 = arg7.iterator();
            while (v2.hasNext()) {
                arg6.insert("aggregated", null, a(v2.next()));
            }
            arg6.setTransactionSuccessful();
            a.b(arg6, "aggregated_cache");
            arg5.a("success", false);
            return true;
        } catch (SQLException v0_1) {
            by.e("insert to Aggregated cache table faild!");
            return false;
        } finally {
            arg6.endTransaction();
        }
    }

    public static boolean b(SQLiteDatabase arg3, String arg4) {
        try {
            if (a.c(arg3, arg4) >= 0) {
                arg3.execSQL("delete from " + arg4);
            }
        } catch (SQLException v0) {
            by.e("cleanTableData faild!" + v0.toString());
            boolean v0_1 = false;
            return v0_1;
        }

        return true;
    }

    public static JSONObject b(SQLiteDatabase arg10) {
        JSONArray v3_1;
        JSONObject v0_2;
        Cursor v2 = null;
        Cursor v1 = null;
        try {
            if (a.c(arg10, "aggregated") <= 0) {
                return null;
            }
            v2 = arg10.rawQuery("select * from aggregated", null);
            v0_2 = new JSONObject();
            while (v2.moveToNext()) {
                try {
                    String v4 = v2.getString(v2.getColumnIndex("key"));
                    if (v0_2.has(v4)) {
                        v3_1 = v0_2.getJSONArray(v4);
                        v0_2.remove(v4);
                    } else {
                        v3_1 = new JSONArray();
                    }

                    JSONObject v5 = new JSONObject();
                    v5.put("v_sum", v2.getLong(v2.getColumnIndex("value")));
                    v5.put("ts_sum", v2.getLong(v2.getColumnIndex("totalTimestamp")));
                    v5.put("tw_num", Integer.parseInt(v2.getString(v2.getColumnIndex("timeWindowNum"))));
                    v5.put("count", v2.getInt(v2.getColumnIndex("count")));
                    v5.put("labels", d.a(v2.getString(v2.getColumnIndex("label"))));
                    v3_1.put(v5);
                    v0_2.put(v4, v3_1);
                    continue;
                } catch (Exception v3) {
                    continue;
                }
            }
            return v0_2;
        } catch (SQLException v0_1) {
            by.e("readAllAggregatedDataForUpload error " + v0_1.toString());
            return null;
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static boolean b(SQLiteDatabase arg7, f arg8) {
        Cursor v1 = null;
        try {
            HashMap v0_2 = new HashMap();
            v1 = arg7.rawQuery("select * from aggregated_cache", null);
            while (v1.moveToNext()) {
                i v2 = new i();
                v2.a(d.b(v1.getString(v1.getColumnIndex("key"))));
                v2.b(d.b(v1.getString(v1.getColumnIndex("label"))));
                v2.c(((long) v1.getInt(v1.getColumnIndex("count"))));
                v2.b(((long) v1.getInt(v1.getColumnIndex("value"))));
                v2.b(v1.getString(v1.getColumnIndex("timeWindowNum")));
                v2.a(Long.parseLong(v1.getString(v1.getColumnIndex("totalTimestamp"))));
                ((Map) v0_2).put(d.b(v1.getString(v1.getColumnIndex("key"))), v2);
            }

            if (((Map) v0_2).size() > 0) {
                arg8.a(v0_2, false);
            } else {
                arg8.a("faild", false);
            }
            return false;
        } catch (SQLException v0_1) {
            arg8.a(Boolean.valueOf(false), false);
            by.e("cacheToMemory happen " + v0_1.toString());
            return false;
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static int c(SQLiteDatabase arg5, String arg6) {
        int v0_1;
        Cursor v2 = null;
        try {
            v2 = arg5.rawQuery("select * from " + arg6, null);
            v0_1 = v2.getCount();
            return v0_1;
        } catch (Exception v1) {
            by.e("count error " + v1.toString());
            return 0;
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
        }

    }

    public static List c(SQLiteDatabase arg5) {
        ArrayList v0_2;
        Cursor v2 = null;
        Cursor v1 = null;
        try {
            if (a.c(arg5, "limitedck") <= 0) {
                return null;
            }

            v2 = arg5.rawQuery("select * from limitedck", null);
            v0_2 = new ArrayList();
            while (v2.moveToNext()) {
                ((List) v0_2).add(v2.getString(v2.getColumnIndex("ck")));
            }
            return v0_2;
        } catch (SQLException v0_1) {
            by.e("loadLimitCKFromDB error " + v0_1.toString());
            return null;
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
        }
    }

    private static void d(SQLiteDatabase arg2, String arg3) {
        try {
            arg2.beginTransaction();
            arg2.execSQL("update system set count=count+1 where key like \'" + arg3 + "\'");
            arg2.setTransactionSuccessful();
        } catch (SQLException v0_1) {

        } finally {
            if (arg2 == null) {
                return;
            }
            arg2.endTransaction();
            return;
        }
    }
}

